<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 交易控制器
 *
 * @author <judasnow@gmail.com>
 */
class Deal extends CI_Controller{

	//当前登录用户的信息
	//若没有用户登录 则为空
	private $subject_user_info;	 

	public function __construct(){
		parent::__construct();
		$this->load->library( 'user_auth' ); 
		if( $this->user_auth->is_login() ){
			//已经登录系统
			//得到用户的信息
			$user = $this->session->userdata( 'user' );
			$user_obj = unserialize( $user['user_obj'] );
			$this->subject_user_info = $user_obj->get();
		 }else{
			$this->subject_user_info = array();
		 }
	}

	//展示一件商品的页面
	//@todo 暂时不需要这个功能
	public function show(){
	/*{{{*/
		//得到需要显示的商品的编号
		$url = $this->uri->uri_to_assoc();
		$product_id = $url['product_id'];
		//判断商品是否存在
		$this->load->model( 'product_model' , '' , TRUE );
		if( !$this->product_model->is_exists( array( 'product_id'=>$product_id ) ) ){
			//数据库中不存在相应 id 的产品
			echo 'There isn`t this product_id in database.';
			exit;
		}
		//读取相应编号商品的介绍页面 
		$this->load->view( 'product_show' , array( 'product_id'=>$product_id , 'subject_user_info'=>$this->subject_user_info ) );
	}/*}}}*/

	//显示购买一件商品的页面
	public function buy(){
	/*{{{*/
		//得到需要显示的商品的编号
		$url = $this->uri->uri_to_assoc();
		$product_id = $url['product_id'];
		//得到商品所属的服务
		$class_id = $url['class_id'];
		if( empty( $this->subject_user_info ) ){
			//若用户还没有登录系统
			$user_addresses = array();	 
		}else{
			$user_id = $this->subject_user_info['user_id'];
			//获得用户当前存在的地址
			$this->load->model( 'user_address_model' , '' , TRUE );
			$user_addresses = $this->user_address_model->find_all( array( 'user_id'=>$user_id ) );
		}
		//获得 product 信息
		$this->load->model( 'product_at_spec_class_model' , '' , TRUE );
		$product_info = $this->product_at_spec_class_model->find(
			array( 'product_id'=>$product_id , 'product_class_id'=>$class_id )
                );
                if( empty( $product_info ) ){
                        show_404();
                }
                //获得 product_class 信息
                //因为服务的中文名称需要填到订单信息中 所以
                //对于已经删除分类下的商品 不能进行交易
		$this->load->model( 'product_class_model' , '' , TRUE );
                $product_class_info = $this->product_class_model->find( array( 'class_id'=>$class_id ) );
                if( empty( $product_class_info ) ){
                        show_404();
                }
		//若有错误信息( resopond )
		$respond = $this->session->flashdata( 'respond' );
		$this->load->view( 'buy' ,
			array( 'product_id'=>$product_id , 'subject_user_info'=>$this->subject_user_info , 'user_info'=>$this->subject_user_info , 
			'user_addresses'=>$user_addresses , 'product_info'=>$product_info , 'respond'=>$respond , 'product_class_info'=>$product_class_info ) );
	}/*}}}*/

	//进行购买操作
	public function do_buy(){
	/*{{{*/
	        //判断用户是否登录
		//没有登录的用户不能进行购买操作
		if( empty( $this->subject_user_info ) ){
			//若用户还没有登录系统 
			header( 'Location: /auth/login/' );
			exit;
		}else{
			$user_id = $this->subject_user_info['user_id'];
		}
		try{
			//若出现异常 product_id 为返回该页的必须条件因此要
			//提前获得
			$product_id = $this->input->post( 'product_id' , TRUE );
			$product_class_id = $this->input->post( 'class_id' , TRUE );
			$product_title = $this->input->post( 'product_title' , TRUE );
			$product_class_name_cn = $this->input->post( 'product_class_name_cn' , TRUE );
			//地址信息
			$address = $this->input->post( 'address' );
			if( empty( $address ) ){
				//使用了新的地址
				//需要添加到数据库中
				$this->load->model( 'user_address_model' , '' , TRUE );
				$address = $this->user_address_model->create_and_save_new_address_item( $user_id );
			}
			//获得交易信息
			//商品的数量
			//@todo 根据库存进行限制 前后台都需要
			$quantity = $this->input->post( 'quantity' , TRUE );
			//商品购买时的单价
			$price = $this->input->post( 'price' , TRUE );
			//总价 数量 * 单价
			$sum_price = $this->input->post( 'sum_price' , TRUE );
			//生成交易记录信息 准备写入数据库
			$new_order_item = array( 
				'user_id' => $user_id ,
				'product_id' => $product_id ,
				'product_title'=>$product_title ,
				'product_class_id' => $product_class_id , 
				'product_class_name_cn'=>$product_class_name_cn ,
				'quantity' => $quantity ,
				'price' => $price ,
				'user_nick'=>$this->subject_user_info['user_nick'] ,
				'sum_price' => $sum_price ,
				'address' => "$address"
                        );
			//添加新的交易记录
			$this->load->model( 'order_model' , '' , TRUE );
			$order_sn = $this->order_model->add_new( $new_order_item );
			//购买成功
			header( "Location: /deal/payment/order_sn/$order_sn"  );
			exit;
		}catch( RunTimeException $e ){
			//运行时异常显示给用户
			$respond = array( 'res'=>'error' , 'info'=>$e->getMessage() );
			$this->session->set_flashdata( 'respond' , $respond );
			header( "Location: /deal/buy/product_id/$product_id/class_id/$product_class_id" );
			exit;
		}catch( Exception $e ){
			show_error( $e->getMessage() );
		}
	}/*}}}*/

	//付款
	public function payment(){
		$uri_info = $this->uri->uri_to_assoc( 3 );
		$order_sn = @$uri_info['order_sn'];
		//如果没有传入 order_sn 
		//显示不是可用的订单
		if( empty( $order_sn ) ){
			$order_info = array();
		}else{
			//判断用户是否登录
			//没有登录的用户不能进行购买操作
			if( empty( $this->subject_user_info ) ){
			        //若用户还没有登录系统 
			        header( 'Location: /auth/login/' );
				exit;
			}else{
				$user_id = $this->subject_user_info['user_id'];
			}
			//获得 order_info 
			$this->load->model( 'order_model' , '' , TRUE );
			$order_info = $this->order_model->find( array( 'order_sn'=>$order_sn , 'user_id'=>$user_id ) );
		}
	        $this->load->view( 'payment' , array( 'order_info'=>$order_info ) );
	}

	//显示订单列表
	public function orders(){
	/*{{{*/
		//判断用户是否登录 
		//没有登录的用户不能进行购买操作
		if( empty( $this->subject_user_info ) ){
			//若用户还没有登录系统 
			show_error( '没有登录.' );		
		}else{		 
			$user_id = $this->subject_user_info['user_id'];
		}
		$this->load->model( 'order_model' , '' , TRUE );
		$total_rows = $this->order_model->count_all( array( 'user_id'=>$user_id ) );

		$this->load->library('pagination');
		$this->config->load('pagination');

		$config = $this->config->item( 'pagination' );
		$config['base_url'] = '/deal/orders?';	 	 
		$config['total_rows'] = $total_rows;

		$this->pagination->initialize( $config );
	        $offset = $this->input->get( 'pager' , TRUE );
		if( empty( $offset ) ){
			$offset = 0;
		}
		$orders = $this->order_model->find_all( 
			array( 'user_id'=>$user_id ) ,
			array( 'title'=>'order_id' , 'order'=>'DESC' ) ,
			$config['per_page'] , 
			$offset
		);
		$pager_links = $this->pagination->create_links();
		//向 order 中添加商品的 title 信息
		$this->load->model( 'product_model' , '' , TRUE );
                foreach( $orders as $index=>$order ){
                        $product_info = $this->product_model->find( array( 'product_id'=>$order['product_id'] ) );
			$orders[$index]['product_info'] = $product_info;
			$orders[$index]['opt'] = '';
			if( $order['status'] == 'BUY' ){			 
				//订单状态如果是 BUY 则
				//显示 付款 取消 链接			
				$orders[$index]['opt'] = '<a href="/deal/payment/order_sn/' . $order['order_sn'] . 
					'">付款</a>|<a href="javascript:void(0);" class="abandon" id="' . $order['order_sn'] . '">取消</a>';
			}
		}
		$this->load->view( 'orders' , 
			array( 'subject_user_info'=>$this->subject_user_info , 'orders'=>$orders , 'pager_links'=>$pager_links ) );

	}/*}}}*/

	//取消订单
	public function ajax_abandon_order(){
		$order_sn = $this->input->post( 'order_sn' , TRUE );
		//判断用户是否登录 
		//没有登录的用户不能进行购买操作
		if( empty( $this->subject_user_info ) ){
			//若用户还没有登录系统 
			show_error( '没有登录.' );		
		}else{
			$user_id = $this->subject_user_info['user_id'];
		}
		//修改订单状态
		$this->load->model( 'order_model' , '' , TRUE );
		try{
			$this->order_model->update( 
				array( 'status'=>'ABANDON' ) ,
				array( 'order_sn'=>$order_sn , 'user_id'=>$user_id ) );
			echo '{"res":"TRUE"}';
		}catch( Exception $e ){
			 echo '{"res":"FALSE"}';
		}	
	} 

	//显示具体的订单信息
	public function order_detail(){
	/*{{{*/
		//判断用户是否登录 
		//没有登录的用户不能进行购买操作
		if( empty( $this->subject_user_info ) ){
			 //若用户还没有登录系统 
			 show_error( '没有登录.' );		
		}else{
			 $user_id = $this->subject_user_info['user_id'];
		}
		//需要判断该订单的所有者 
		//是否为当前的用户
		$order_id = $this->uri->segment( 3 , 0 );
		//获得该 order_id 的信息
		$this->load->model( 'order_model' , '' , TRUE );
		//获得该用户所有的订单
		$order = $this->order_model->find( array( 'order_id'=>$order_id ) , TRUE );
		$this->load->view( 'order_detail' , array( 'subject_user_info'=>$this->subject_user_info ) );
	}/*}}}*/
}
